package cn.edu.njust.epms.service.Impl;

import cn.edu.njust.epms.dao.HRDao;
import cn.edu.njust.epms.dao.UserDao;
import cn.edu.njust.epms.entity.HRRecord;
import cn.edu.njust.epms.entity.UserDetails;
import cn.edu.njust.epms.service.HRService;
import cn.edu.njust.epms.common.R;
import cn.edu.njust.epms.common.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Service
public class HRServiceImpl implements HRService {
    @Autowired
    HRDao dao;
    @Autowired
    UserDao udao;
    @Override
    public Resp<HRRecord> HealthReport(String pid, Boolean uTemp) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
//        System.out.println(calendar.getTime());
        Date date1 = calendar.getTime();
        calendar.add(Calendar.DATE,1);
        List<UserDetails> list = dao.selectUdHR(date1,calendar.getTime());
        for (UserDetails details : list) {
            if (details.getPid().trim().equals(pid.trim()))
                return Resp.error("您今日已经完成上报，请明天再来！");
        }

        HRRecord record = new HRRecord();
        record.setrTime(new Date());
        record.setPid(pid);
        record.setuTemp(uTemp);
        boolean isOk = dao.Add(record);
        if(isOk){
            return Resp.success(record);
        }else
            return Resp.error("上报失败！");
    }

    @Override
    public Resp<List<HRRecord>> selectByCondition(R r){
        if(!(r.getName() == null||r.getName().equals("")||"".equals(r.getName())))
            r.setName("%"+r.getName()+"%");
        if(!(r.getPid() == null||r.getPid().equals("")||"".equals(r.getPid())))
            r.setPid("%"+r.getPid()+"%");
//        PageHelper.startPage(pageNo,size,order);
        List<HRRecord> list = dao.selectByCondition(r);
//        PageInfo<HRRecord> pageInfo = new PageInfo<>(list);
//        System.out.println(pageInfo);
        return Resp.success(list);
    }

    @Override
    public Resp<String> deleteHRRecord(HRRecord r){
        Boolean b = dao.deleteHRRecord(r);
        if (b)  return Resp.success("删除成功");
        return Resp.error("删除失败！");
    }

    @Override
    public List<UserDetails> selectUdNotHRToday() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
//        System.out.println(calendar.getTime());
        Date date1 = calendar.getTime();
        calendar.add(Calendar.DATE,1);
//        SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");
        List<UserDetails> list = dao.selectUdWithoutHR(date1, calendar.getTime());
        return list;
    }
}
